package com.eurasia.assignment.infrastructure.client.hoj.resources;

import com.eurasia.assignment.controller.dto.CreateCodeSubmissionResponse;
import com.eurasia.assignment.controller.dto.ProblemQuery;
import com.eurasia.assignment.domain.hoj.CodeInfo;
import com.eurasia.assignment.service.SecurityContext;
import com.eurasia.assignment.service.port.out.HOJResourcePort;
import com.eurasia.assignment.service.port.out.dto.ConclusionJudgementResult;
import com.eurasia.assignment.service.port.out.dto.CreateFullCaseJudgementRequest;
import com.eurasia.assignment.service.port.out.dto.CreateFullCaseJudgementResponse;
import com.eurasia.assignment.service.port.out.dto.HOJResource;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class HOJResourceClientAdapter implements HOJResourcePort {
    private final HOJResourceFeignClient hojResourceFeignClient;
    private final ObjectMapper objectMapper;
    private final SecurityContext securityContext;
    
    private ObjectMapper customMapper;
    
    @PostConstruct
    public void init() {
        // 创建一个自定义的ObjectMapper，专门用于灵活解析响应
        customMapper = objectMapper.copy();
        customMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @Override
    public ResponseEntity<String> getProblemDetail(String id) {
        return hojResourceFeignClient.getProblemDetail(id);
    }

    @Override
    public ResponseEntity<String> getProblems(ProblemQuery query) {
        return hojResourceFeignClient.getProblemList(query);
    }

    @Override
    public ResponseEntity<CreateCodeSubmissionResponse> createCodeSubmission(CodeInfo body) {
        return hojResourceFeignClient.createCodeSubmission(body);
    }

    @Override
    public ResponseEntity<String> getTestJudgementResult(String testJudgeKey) {
        return hojResourceFeignClient.getTestJudgementResult(testJudgeKey);
    }

    @Override
    public ResponseEntity<HOJResource<CreateFullCaseJudgementResponse>> createFullCaseJudgement(CreateFullCaseJudgementRequest request) {

        ResponseEntity<String> fullCaseJudgement = hojResourceFeignClient.createFullCaseJudgement(request);
        if (fullCaseJudgement.getStatusCode().is2xxSuccessful()) {
            try {
                // 使用TypeReference保留完整的泛型类型信息
                // TODO 新知识
                TypeReference<HOJResource<CreateFullCaseJudgementResponse>> typeRef =
                    new TypeReference<HOJResource<CreateFullCaseJudgementResponse>>() {};
                HOJResource<CreateFullCaseJudgementResponse> resource =
                    customMapper.readValue(fullCaseJudgement.getBody(), typeRef);
                return ResponseEntity.ok(resource);
            } catch (JsonProcessingException e) {
                log.error("解析判题结果失败: {}", e.getMessage(), e);
                return ResponseEntity.internalServerError().build();
            }
        }
        return ResponseEntity.status(fullCaseJudgement.getStatusCode()).build();
    }

    @Override
    public ResponseEntity<String> fetchFullCaseJudgementConclusionResult(Long submitId) {
        return hojResourceFeignClient.fetchFullCaseJudgementConclusionResult(submitId);
//        if (response.getStatusCode().is2xxSuccessful()) {
//            try {
//                // TODO 新知识 使用TypeReference保留完整的泛型类型信息
//                TypeReference<HOJResource<ConclusionJudgementResult>> typeRef = new TypeReference<>() {};
//                HOJResource<ConclusionJudgementResult> result = customMapper.readValue(response.getBody(), typeRef);
//                return ResponseEntity.ok(result);
//           } catch (JsonProcessingException e) {
//                log.error("解析判题结果失败: {}", e.getMessage(), e);
//                return ResponseEntity.internalServerError().build();
//            }
//        }
//        return ResponseEntity.status(response.getStatusCode()).build();
    }

    @Override
    public ResponseEntity<String> fetchFullCaseJudgementDetailedResult(Long submitId) {
        return hojResourceFeignClient.fetchFullCaseJudgementDetailedResult(submitId);
    }
}
